<template>
  <grid>
    <template slot="left">
      <div class="left-side__body">
        <el-tooltip effect="light"
                    content="点赞"
                    placement="left">
          <span @click="approveArticle(article.id)"><i class="iconfont">{{ isApprove ? '&#xe601;' : '&#xe680;'}}</i></span>
        </el-tooltip>
        <el-tooltip effect="light"
                    :content="isCollect? '已收藏' : '收藏'"
                    placement="left">
          <span @click="collectArticle(article.id)"><i class="iconfont">{{ isCollect ? '&#xe8cb;' : '&#xe8ca;'}}</i></span>
        </el-tooltip>
        <el-tooltip effect="light"
                    content="评论"
                    placement="left">
          <span @click="scrollToComment"><i class="el-icon-chat-dot-round"></i></span>
        </el-tooltip>
        <el-tooltip effect="light"
                    content="分享"
                    placement="left">
          <span><i class="el-icon-position"></i></span>
        </el-tooltip>
        <el-tooltip effect="light"
                    content="举报"
                    placement="left">
          <span @click="reportArticle(article.id)"><i class="el-icon-warning-outline"></i></span>
        </el-tooltip>
      </div>
    </template>
    <div class="article-content__body">
      <el-alert v-if="!isOnline"
                title="当前为管理员预览模式 普通用户无法查看到该内容"
                type="warning"
                show-icon
                :closable="false">
      </el-alert>
      <div class="header">
        <span class="reproduced"
              v-show="isReproduced">转</span>
        <h1>{{article.title}}</h1>
        <div class="trivial-body">
          <div class="basic-info">
            <span class="original-link"
                  v-show="isReproduced">
              <a :href="article.original_link">
                <i class="el-icon-link"></i>原文链接
              </a>
            </span>
            <span class="original-link">
              <router-link :to="{
                name:'search',
                query:{
                  cate: `${article.cate_id}`
                }
              }">
                {{article.cate.name}}
              </router-link>
            </span>
            <span>
              <i class="el-icon-view"></i>{{ article.view_nums }}
            </span>
            <span>
              <i class="el-icon-chat-dot-round"></i>{{ article.comment_nums }}
            </span>
            <span class="time-publish">
              <el-tooltip effect="dark"
                          :content="article.created_at | timeFilter"
                          placement="top">
                <font>发布于 {{article.created_at | timeFilter('before')}}</font>
              </el-tooltip>
            </span>
          </div>
          <div class="tag-list">
            <span v-for="(tag,index) in article.tag_list"
                  :key="index"
                  class="tag-body">#{{tag.name}}</span>
          </div>
        </div>
      </div>
      <el-divider class="divider"></el-divider>
      <div class="summary">{{article.summary}}</div>
      <content-container ref="contentContainer"
                         :content="article.content"
                         :is-marked="isMarkDown"
                         @menuChange="buildMenuList"></content-container>
      <div class="footer"
           v-if="isReproduced">
        <p>本文为转载内容，转载自<a :href="article.original_link">原文</a>。
          转载目的在于学习、记录及传递更多信息。</p>
        <p>如涉及作品内容、版权或其它问题，请点击左侧举报按钮选择侵权然后提交，我们将在第一时间删除内容。</p>
      </div>
    </div>
    <comment-list ref="comment"
                  :item_id="id"
                  :can_comment="canComment"
                  :comment_nums="article.comment_nums">
    </comment-list>
    <template slot="right-avatar">
      <div style="margin-top: 30px"></div>
    </template>
    <template slot="right">
      <divider :width="60"
               align="center">
      </divider>
      <div class="menu-list__content"
           ref="menu">
        <div class="menu-header">
          <ul>
            <li v-for="(menu,index) in menuList"
                :key="index"
                :class="{'menu-active':index === checked}"
                @click="scrollToMenu(index)">
              {{menu.title}}
            </li>
          </ul>
        </div>
      </div>
    </template>
  </grid>
</template>

<script>
import { mapActions, mapGetters } from 'vuex'
import commentList from '@/components/comment-list'
import { scrollToSlow, getScrollTop } from '@/utils/browser'
import contentContainer from '@/components/content-container'

export default {
  name: 'topic-article',
  props: {
    id: {
      type: String,
      default: ''
    },
  },
  title () {
    return this.article.title
  },
  keywords () {
    return this.article.keywords
  },
  description () {
    return this.article.summary
  },
  data () {
    return {
      menuList: [],
      menuLength: 0,
      nowScrollTop: 0,
      checked: 0,
      scrollHeight: 0,
    }
  },
  async asyncData ({ store, route }) {
    return store.dispatch('topic/fetchView', route.params.id)
  },
  components: {
    commentList, contentContainer
  },
  computed: {
    ...mapGetters('topic', ['article']),
    isReproduced () {
      return this.article.copyright === 1
    },
    isMarkDown () {
      return this.article.type === 1
    },
    canComment () {
      return this.article.allow_comment === 1
    },
    isCollect () {
      return this.article.is_collect
    },
    isApprove () {
      return this.article.is_approve
    },
    isOnline () {
      return this.article.status === 1
    }
  },
  mounted () {
    window.addEventListener('scroll', this.handleScroll);
  },
  watch: {
    scrollHeight (newVal, oldVal) {
      if (newVal !== oldVal) {
        this.$nextTick(() => {
          this.$refs.contentContainer.buildMenuList()
        });
      }
    }
  },
  methods: {
    ...mapActions('topic', ['collect', 'approve']),
    async collectArticle (id) {
      await this.collect(id)
      this.$message({
        message: this.isCollect ? '收藏成功 你可在个人中心找到这篇文章' : '你已取消收藏',
        type: 'success',
        offset: 10
      });
    },
    async approveArticle (id) {
      await this.approve(id)
      this.$message({
        message: this.isApprove ? '点赞成功 你可在个人中心找到这篇文章' : '你已取消点赞',
        type: 'success',
        offset: 10
      });
    },
    async reportArticle (id) {
      this.$reportOpen('item', { id: this.id, title: this.article.title });
    },
    handleScroll () {
      this.scrollHeight = document.body.scrollHeight;
      this.nowScrollTop = getScrollTop()

      if (this.menuLength === 0) {
        return false
      }
      if (this.nowScrollTop < this.menuList[0].offsetTop) {
        this.checked === 0 ? '' : this.checkActived(0)
        return false;
      }
      if (this.nowScrollTop > this.menuList[this.menuLength - 1].offsetTop) {
        this.checked === this.menuLength - 1 ? '' : this.checkActived(this.menuLength - 1)
        return false;
      }
      this.menuList.forEach((item, index) => {
        if (this.nowScrollTop > item.offsetTop && this.nowScrollTop <= this.menuList[index + 1].offsetTop) {
          this.checkActived(index)
        }
      })
    },
    scrollToMenu (checked) {
      scrollToSlow(this.menuList[checked].offsetTop + 15)
    },
    checkActived (checked) {
      this.checked = checked
    },
    scrollToComment () {
      scrollToSlow(this.$refs.comment.$el.offsetTop + 15)
    },
    buildMenuList (menuList = []) {
      this.menuList = menuList
      this.menuLength = menuList.length
      this.handleScroll()
    }
  }
}
</script>

<style lang="scss" scoped>
@import "./content.scss";
.left-side__body {
  position: sticky;
  top: 30px;
  margin-top: 13px;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  span {
    display: flex;
    cursor: pointer;
    width: 35px;
    height: 35px;
    justify-content: center;
    background-color: #f6f6f6;
    border-radius: 50%;
    &::before {
      content: "";
    }
    i {
      line-height: 35px;
    }
  }
  :nth-child(n + 2) {
    margin-top: 15px;
  }
}
.article-content__body {
  display: flex;
  position: relative;
  flex-direction: column;
  justify-content: flex-start;
  padding: 0 10px;
  margin-right: -15px;
  margin-left: 10px;
  text-align: left;
  background-color: #fff;
  .header {
    position: relative;
    font-weight: 500;
    color: #6c757d;
    .reproduced {
      position: absolute;
      top: 8px;
      left: -15px;
      font-size: 13px;
      color: #409eff;
    }
    h1 {
      margin: 8px 0 15px 0;
    }
    .trivial-body {
      display: flex;
      justify-content: space-between;
      color: #999;
      padding: 3px 0;
      font-size: 13px;
      .tag-list {
        .tag-body {
          cursor: pointer;
        }
        span:nth-child(n + 2) {
          margin-left: 8px;
        }
      }
      .basic-info {
        .original-link {
          cursor: pointer;
          a {
            color: #999;
            text-decoration: none;
          }
        }
        .time-publish {
          cursor: pointer;
        }
        span > i {
          margin: 0 5px 0 0;
        }
        span::after {
          content: " / ";
        }
        span:last-child::after {
          content: "";
        }
      }
    }
  }
  .divider {
    border: none;
    margin: 4px 0 5px 0;
  }
  .summary {
    font-style: italic;
    background: #f6f6f6;
    color: #adadad;
    border-radius: 5px;
    text-align: justify;
    word-wrap: break-word;
    padding: 10px;
    margin-top: 10px;
    font-size: 14px;
  }
  .footer {
    color: #3c763d;
    background-color: #dff0d8;
    border-color: #d6e9c6;
    font-size: 13px;
    padding: 15px;
    margin: 20px 0;
    border: 1px solid transparent;
    border-radius: 4px;
    p {
      margin: 0;
      a {
        color: #3c763d;
      }
    }
    p:nth-child(1) {
      margin-bottom: 5px;
    }
  }
  .el-divider__text {
    color: #6c757d;
  }
}
.menu-list__content {
  position: sticky;
  top: 30px;
  display: flex;
  z-index: 1;
  margin-left: 8%;
  flex-direction: column;
  text-align: left;
  ul {
    margin: 8px 0;
    list-style: none;
    font-size: 14px;
    color: #595959;
    li {
      cursor: pointer;
      padding: 0 10px 0 16px;
      border-left: 2px solid #e8e8e8;
    }
  }
  .menu-active {
    color: #409eff;
    border-left: 2px solid #409eff;
  }
}
</style>